package oop.core.view.listeners;

import oop.core.med.InventoryManagementMediator;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * The Inventory mouse listener is activated when the user clicks 
 * within the inventory. It then determines which item is associated with the 
 * click and sends the key code to the correct mediator.
 * @author Alex
 *
 */
public class InventoryMouseListener implements MouseListener {
	InventoryManagementMediator inventoryManagementMediator;

	public InventoryMouseListener() {
		inventoryManagementMediator=null;
	}
	
	public void setInventoryManagementMediator(InventoryManagementMediator inventoryManagementMediator)
	{
		this.inventoryManagementMediator = inventoryManagementMediator;
	}

	@Override
	/**
	 * Method takes in the x and y coordinates from the MouseEvent and 
	 * determines the code of the item.
	 */
	public void mouseClicked(MouseEvent e) {
		int x=e.getX();
		int y=e.getY();
		
		InventoryIndexCalculator calc = new InventoryIndexCalculator();
		int code = calc.getCode(x, y);
		if(code == -1)	// Didn't press an item.
			return;
		
		int clickCode = e.getButton();
		if(clickCode==MouseEvent.BUTTON1){
			inventoryManagementMediator.useItem(code);
		}
		else if(e.isMetaDown()){
			inventoryManagementMediator.dropItem(code);
		}
	}
	
	@Override
	public void mousePressed(MouseEvent e) {
	}
	

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}

}
